﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;


public partial class sysadmin_SystemInfo_CourseTeacherStudylog : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["OrderList"] = "";  //
            ViewState["OrderType"] = " desc ";//排序方式
            BinderDDl();
            string search = GetSearch();
            //Response.Write(search);
            //Response.End();
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_CourseTeacherStudylog_InfoTitle.Text = GetLanageValueByKey("lbl_CourseTeacherStudylog_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_ProjectName.Text = GetLanageValueByKey("lbl_ProjectName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_ClassName.Text = GetLanageValueByKey("lbl_ClassName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseName.Text = GetLanageValueByKey("lbl_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_RealName.Text = GetLanageValueByKey("lbl_RealName", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnExport.Text = GetLanageValueByKey("msg_DateToExcel", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append("select * from VM_TeacherStudyProjectCourseLog");

        sbSearch.Append(" where 1=1 ");
        string sproid = ddl_Project.SelectedValue.ToString();
        if (sproid.Length > 0)
            sbSearch.Append("and TrainproID="+sproid+"");
        if (txt_Class.Text.Length > 0)
            sbSearch.Append(" and (UserId in (SELECT DISTINCT  dbo.TUser.UserId FROM dbo.TUser WHERE (dbo.TUser.UserType = 0) AND (dbo.TUser.UserId IN (SELECT FKTrainclastuUserID  FROM TTrainClassStudent WHERE FKTrainclastuTrainclaID IN (SELECT TrainclaID FROM TTrainClass where TrainclaName like '%" + txt_Class.Text + "%'))) OR (dbo.TUser.UserId IN (SELECT FKTeacherUserId FROM TTeacher WHERE FKTeacherDepartmentId IN (SELECT FKTraincladeptDepartmentID  FROM TTrainClassDept WHERE FKTraincladeptTrainclaID IN (SELECT TrainclaID FROM TTrainClass  where TrainclaName like '%" + txt_Class.Text + "%'))))))");
        if (txt_Course.Text.Length > 0)
            sbSearch.Append(" and CourseName like '%"+txt_Course.Text+"%'");
        if (txt_RealName.Text.Length > 0)
            sbSearch.Append(" and TeacherName like '%" + txt_RealName.Text + "%'");
        //sbSearch.Append(" 1=1 ");

        return sbSearch.ToString();
    }

    private void BinderDDl()
    {
        DataTable dt = new DataTable();
        PowerCreator.LMS.Web.Project.Project opProject = new PowerCreator.LMS.Web.Project.Project();
        dt = opProject.QueryTrainProject("");
        ddl_Project.DataSource = dt;
        ddl_Project.DataTextField = "TrainproName";
        ddl_Project.DataValueField = "TrainproID";
        ddl_Project.DataBind();

    }

    //private void BinderPager(string search)
    //{
    //    AspNetPager1.RecordCount = obj.Count(search);
    //    AspNetPager1.PageSize = LMSPageSize;
    //}

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        int pagecount = obj.Count(search);

        string orderlist = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
            search += " order by " + orderlist + ViewState["OrderType"].ToString();
        else
            search += " order by TrainproID desc,CourseId desc,UserId desc";

        //string orderList = " OnlineuserLoginTime desc ";

        //Response.Write(search);
        //Response.End();
        dt = obj.Query(search, pagecount, pageIndex);

        dt.Columns.Add("StrStudyAllTime");
        dt.Columns.Add("strUrl");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["StrStudyAllTime"]=PowerCreator.LMS.Common.Utils.TimeUtils.GetTimeStr(dt.Rows[i]["StudyAllTime"].ToString());
            dt.Rows[i]["strUrl"] = "userid=" + dt.Rows[i]["UserId"].ToString() + "&courseid=" + dt.Rows[i]["Course_Id"].ToString() + "&projectid=" + dt.Rows[i]["TrainproID"].ToString();

        }
        ViewState["dt"] = dt;

        rpt_StudentClassLoginNum.DataSource = dt;
        rpt_StudentClassLoginNum.DataBind();
    }

     protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {
        
    }

    protected void rpt_StudentClassLoginNum_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "orderby")
        {
            ViewState["OrderList"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, 1);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }

        else if (e.CommandName.ToLower() == "lshow")
        {
            string url = string.Format(WebUtils.WebRoot + "/sysadmin/SystemInfo/UserStudeyCourseLog.aspx?{0}", e.CommandArgument.ToString());
            //string url = string.Format("StudentLoginNum.aspx");
            //string url = string.Format("StudentLoginNum.aspx?TrainclaID={0}", e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }

    }
    protected void btnExport_Click(object sender, EventArgs e)
    {

        DataTable dt = (DataTable)ViewState["dt"];
        ExportList(dt);
    }

    private void ExportList(DataTable dt)
    {
        ArrayList list = new ArrayList();
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (dt.Columns[i].ColumnName == "TeacherName")
            {
                dt.Columns[i].ColumnName = "教师";
                dt.Columns[i].SetOrdinal(0);
            }
            else if (dt.Columns[i].ColumnName == "CourseName")
            {
                dt.Columns[i].ColumnName = "课程";
                dt.Columns[i].SetOrdinal(1);
            }
            else if (dt.Columns[i].ColumnName == "StudyNum")
            {
                dt.Columns[i].ColumnName = "学习总次数";
                dt.Columns[i].SetOrdinal(2);
            }
            else if (dt.Columns[i].ColumnName == "StrStudyAllTime")
            {
                dt.Columns[i].ColumnName = "总学习时间";
                dt.Columns[i].SetOrdinal(3);
            }
            else
            {
                list.Add(dt.Columns[i].ColumnName);
            }

        }
        for (int i = 0; i < list.Count; i++)
        {
            dt.Columns.Remove(list[i].ToString());
        }
        PowerCreator.LMS.Common.Utils.Excel objExcel = new PowerCreator.LMS.Common.Utils.Excel();
        //Excel objExcel = new Excel();
        objExcel.ToExcel(dt, this.Page, "教师课程学习统计", "教师课程学习统计", "CourseTeacherStudylog");
    }

}
